var _ = require("lodash");

var errors = require('../exception/exceptionCon').errors;
var warns = require('../exception/exceptionCon').warns;

var authCheck = require('../utils/authCheck');
var userModel = require('../model/userDao').UserModel;

var CLIENT_1 = "CLIENT_1"; // 用户校验场景1，注册使用
var CLIENT_2 = "CLIENT_2"; // 用户校验场景2，确认买车

var registerCon = {

    register: function(req, res, next) {
        console.log(req.url);
        res.render('register', {});
    },

    registerOrLogin: function(req, res, next) {
        var acc = req.body.account;
        var pwd = req.body.pwd;
        console.log(acc, pwd);
        userModel.findOne({ account: acc }, function(err, doc) {
            if (err) return next(errors.SYSTEM_ERROR(res));
            if (doc) return next(errors.EXISTED(res));

            userModel.create({ account: acc, pwd: pwd, createDt: Date.parse(new Date().toLocaleString()) },
                function(err, doc) {
                    if (err) return next(errors.SYSTEM_ERROR(res));
                    authCheck.create({ _id: doc._id, name: acc }, function(err, reply) {
                        if (err) return next(errors.SYSTEM_ERROR(res, "申请权限失败"));
                        res.json({ meta: { status: 1, message: "注册成功" }, data: { token: reply.token } });
                    });
                });
        });

        // veriCodeCheck.verify(mobile, CLIENT_1, code, function(err, reply) {
        //     // 校验不通过
        //     if (err) return next(errors.VERIFY_ERROR(res, reply.message));
        //     clientCon.findOneOrCreate(mobile, function(err, reply) {
        //         if (err) return next(errors.SYSTEM_ERROR(res));
        //         if (!reply) return next(warns.NOT_EXISTED(res));
        //
        //         authCheck.create({ _id: reply._id, name: reply.name }, function(err, reply) {
        //             if (err) return next(errors.SYSTEM_ERROR(res, "申请权限失败"));
        //             res.json({ meta: { status: 1, message: "登录成功" }, data: { token: reply.token } });
        //         });
        //     });
        // });
    },

};


module.exports = registerCon;
